package com.lxy.mychat.Adapter;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.lxy.mychat.Bean.Msg;
import com.lxy.mychat.R;

import java.util.List;

public class MsgAdapter extends ArrayAdapter<Msg> {
    private int resourceId;
    public MsgAdapter(@NonNull Context context,
                       int textViewResourceId, @NonNull List<Msg> objects) {
        super(context, textViewResourceId, objects);
        resourceId=textViewResourceId;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Msg m= getItem(position);
        View view = LayoutInflater.from(getContext()).inflate(resourceId,
                parent, false);
        LinearLayout leftLayout = view.findViewById(R.id.left_layout);
        LinearLayout rightLayout = view.findViewById(R.id.right_Layout);
        TextView left_msg = view.findViewById(R.id.left_msg);
        TextView right_msg = view.findViewById(R.id.right_msg);
        SharedPreferences sp = getContext().getSharedPreferences("userinfo",Context.MODE_PRIVATE);
        int userid=sp.getInt("userid",0);
        if (userid==m.getSend_id()){
            leftLayout.setVisibility(View.VISIBLE);
            rightLayout.setVisibility(View.GONE);
            left_msg.setText(m.getContent());
        }else {
            rightLayout.setVisibility(View.VISIBLE);
            leftLayout.setVisibility(View.GONE);
            right_msg.setText(m.getContent());
        }
        return view;
    }
}
